package com.moon.game.service;

import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

import com.moon.game.cache.Memcached;
import com.moon.game.constants.Constants;
import com.moon.game.constants.GlobalConstants;
import com.moon.game.vos.MsgVo;

/**
 * @description 定时获取世界消息及动态
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-4-28 下午6:18:44
 */
public class ChatTask {
	public static ChatTask insc = new ChatTask();
	private Timer timer = new Timer();
	private int GET_MSG_PERIOD = 3 * 1000;
	private ChatTask() {
	}
	
	public void init() {
		insc.startTask();
	}
	private void startTask() {
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				//世界聊天
				String key = Constants.CHAT_WORLD_KEY;
				LinkedList<byte[]> datas = (LinkedList<byte[]>) Memcached.get(GlobalConstants.POOL_NAME_CHAT, key);
				LinkedList<MsgVo> msgVos = new LinkedList<MsgVo>();
				if (datas != null && datas.size() > 0) {
					for (byte[] d : datas) {
						msgVos.addLast(ChatService.msgVoFactory(d));
					}
				}
				ChatService.setWorldChatList(msgVos);
				
				//世界动态
				key = Constants.CHAT_WORLD_ACT_KEY;
				LinkedList<byte[]> actDatas = (LinkedList<byte[]>) Memcached.get(GlobalConstants.POOL_NAME_CHAT, key);
				LinkedList<MsgVo> msgActVos = new LinkedList<MsgVo>();
				if (actDatas != null && actDatas.size() > 0) {
					for (byte[] d : actDatas) {
						msgActVos.addLast(ChatService.msgVoFactory(d));
					}
				}
				ChatService.setWorldActChatList(msgActVos);
			}
		}, 1, GET_MSG_PERIOD);
	}
	
}
